<?php

namespace apiend\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * ActivitiesSearch represents the model behind the search form of `common\models\original\Activities`.
 */
class SearchModel extends \yii\base\Model {

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params = []) {
        $modelClass = static::instance()->searchModel;
        $query = $modelClass::find()->where([
            'status' => $modelClass::STATUS_ACTIVE,
            'deleted_at' => 0,
        ]);
        
        
        $query->with('user');



//        {
//
//    "id": 827,
//    "avatar": "https://img.lnts.cn/attachments/portrait/201701/17/103652BbMj7CNZwICgULMtlc.jpg",
//    "time": "16小时前",
//    "format": "headline",
//    "text": "正常情况动态",
//    "pic_urls": [ ],
//    "tools": {
//        "like": 0,
//        "reply": 3,
//        "view": 27
//    },
//    "liked": false,
//    "collected": false,
//    "videourl": "",
//    "redirect": "https://daas.lnts.cn/m/headline/dynamic/show/827",
//    "user": {
//        "id": 1036663592,
//        "screen_name": "杨毅",
//        "name": "杨毅",
//        "description": "我正在城楼观山景，耳听得城外乱纷纷。",
//        "profile_image_url": "http://tva3.sinaimg.cn/crop.0.0.180.180.50/3dca3b28jw1e8qgp5bmzyj2050050aa8.jpg",
//        "cover_image_phone": "http://ww1.sinaimg.cn/crop.0.0.640.640.640/549d0121tw1egm1kjly3jj20hs0hsq4f.jpg",
//        "gender": "m",
//        "followers_count": 5928365,
//        "friends_count": 638,
//        "following": true,
//        "verified": true,
//        "verified_type": 0,
//        "avatar_large": "http://tva3.sinaimg.cn/crop.0.0.180.180.180/3dca3b28jw1e8qgp5bmzyj2050050aa8.jpg",
//        "avatar_hd": "http://tva3.sinaimg.cn/crop.0.0.180.180.1024/3dca3b28jw1e8qgp5bmzyj2050050aa8.jpg",
//        "verified_reason": "著名篮球评论员 微博签约自媒体",
//        "follow_me": false,
//        "credit_score": 80
//    }
//
//},
//        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 1,
            ],
        ]);

        if ($this->load($params) && $this->validate()) {
            $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'scope', $this->scope]);
            if ($this->start_at == 0 && $this->start_at != '') { //活动未开始
                $query->andWhere(['>', 'start_at', $now_time]);
            }
            if ($this->start_at == 1) { //活动进行中
                $query->andWhere(['<=', 'start_at', $now_time]);
                $query->andWhere(['>', 'end_at', $now_time]);
            }
            if ($this->start_at == 2) { //活动结束
                $query->andWhere(['<=', 'end_at', $now_time]);
            }
            if (isset($params['union'])) {
                $query->andFilterWhere([
                    'union_id' => $params['union'],
                ]);
            } else {
                $query->andFilterWhere([
                    'type' => $this->type,
                ]);
            }
            // grid filtering conditions
            $query->orderBy('created_at desc');
        }

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

}
